﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CodeTable
{
    public class Utils
    {
        public static void MakeScintillaClone(ScintillaNET.Scintilla source, ScintillaNET.Scintilla destination)
        {
            destination.Location = source.Location;
            destination.BorderStyle = source.BorderStyle;
            destination.Margins.Left = source.Margins.Left;
            destination.Margins.Right = source.Margins.Right;
            for (int i = 0; i < source.Margins.Count; ++i)
            {
                destination.Margins[i].Width = source.Margins[i].Width;
            }
            destination.ConfigurationManager.Language = source.ConfigurationManager.Language;
            destination.Font = (System.Drawing.Font)source.Font.Clone();
            destination.Indentation.BackspaceUnindents = source.Indentation.BackspaceUnindents;
            destination.Indentation.IndentWidth = source.Indentation.IndentWidth;
            destination.Indentation.ShowGuides = source.Indentation.ShowGuides;
            destination.Indentation.SmartIndentType = source.Indentation.SmartIndentType;
            destination.Indentation.TabIndents = source.Indentation.TabIndents;
            destination.Indentation.TabWidth = source.Indentation.TabWidth;
            destination.Indentation.UseTabs = source.Indentation.UseTabs;
            destination.Scrolling.HorizontalWidth = source.Scrolling.HorizontalWidth;
            destination.Scrolling.ScrollBars = source.Scrolling.ScrollBars;
            destination.Size = source.Size;
            destination.Visible = source.Visible;
        }

        public static ScintillaNET.Scintilla GetCloneScintilla(string name, ScintillaNET.Scintilla source)
        {
            ScintillaNET.Scintilla result = new ScintillaNET.Scintilla();
            result.Name = name;
            MakeScintillaClone(source, result);
            return result;
        }

        static Random random = new Random(DateTime.Now.Millisecond);

        public static string GetRandomCode()
        {
            string[] lines = { 
                "for (i = 1; i < 10; ++i) do_ALL();", "while (true) {i++; j -= 5;}", "function BuildThemAlllll() { ... }",
                "int i = 0;", "float f;", "continue;", "break;", "// do!!!", "var v;", "n = null;", "++i;", "j++;", "foo(bar());",
                "if (textSize.Width < 100) textSize.Width = 100;",
                "if (textSize.Height < 20) textSize.Height = 20;",
                "textSize.Width = 1 + 16 + codeBlock.Margins.Left + textSize.Width + codeBlock.Margins.Right + 1;",
                "textSize.Height = 1 + textSize.Height + 1;",
                "if (codeBlock.ClientSize != textSize) codeBlock.ClientSize = textSize;"
            };
            int linesCount = random.Next(1,10);
            string result = "";
            for (int i = 0; i < linesCount; ++i)
            {
                result += lines[random.Next(lines.Length - 1)];
                if (i < linesCount - 1)
                {
                    result += "\n";
                }
            }
            return result;
        }

        public static void ResizeArray<T>(ref T[,] originalArray, int newColumnCount, int newRowCount)
        {
            var newArray = new T[newColumnCount, newRowCount];
            int originalRowCount = originalArray.GetLength(1);
            int originalColumnsCount = originalArray.GetLength(0);
            for (int column = 0; column < originalColumnsCount; ++column)
            {
                Array.Copy(
                    originalArray, column * originalRowCount,
                    newArray, column * newRowCount, 
                    originalRowCount
                );
            }
            originalArray = newArray;
        }

        public static void AppendRowToArray<T>(ref T[,] originalArray)
        {
            ResizeArray<T>(ref originalArray, originalArray.GetLength(0), originalArray.GetLength(1) + 1);
        }
    }
}
